  <template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="楼/楼层" prop="floor" v-if="dataForm.type === 0">
        <el-input v-model="dataForm.floor" placeholder="楼/楼层"/>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentId" v-if="dataForm.type === 1">
        <el-select v-model="dataForm.parentId" placeholder="所属楼/楼层">
          <el-option v-for="item in superior" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="房类" prop="name" v-if="dataForm.type === 1">
        <el-input v-model="dataForm.name" placeholder="房类"/>
      </el-form-item>
      <el-form-item label="价格" prop="price" v-if="dataForm.type === 1">
        <el-input v-model="dataForm.price" type="number" style="width: 50%" placeholder="价格">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="节日价格" prop="disPrice" v-if="dataForm.type === 1">
        <el-input v-model="dataForm.disPrice" type="number" style="width: 50%" placeholder="节日价格">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" hidden>
        <el-input v-model="dataForm.createTime" placeholder="创建时间"/>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime" hidden>
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"/>
      </el-form-item>
      <el-form-item label="逻辑删除  -1：已删除  0：未删除 " prop="isDelete" hidden>
        <el-input v-model="dataForm.isDelete" placeholder="逻辑删除  -1：已删除  0：未删除 "/>
      </el-form-item>
      <el-form-item label="自定义字段" prop="def0" hidden>
        <el-input v-model="dataForm.def0" placeholder="自定义字段"/>
      </el-form-item>
      <el-form-item label="自定义字段" prop="def1" hidden>
        <el-input v-model="dataForm.def1" placeholder="自定义字段"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        radio: 0,
        typeList: ['楼/楼层', '房类'],
        dataForm: {
          id: 0,
          floor: '',
          name: '',
          price: '',
          disPrice: '',
          parentId: '',
          type: 0,
          createTime: '',
          updateTime: '',
          isDelete: '',
          def0: '',
          def1: ''
        },
        dataRule: {
          floor: [
            {required: true, message: '楼/楼层不能为空', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '房类不能为空', trigger: 'blur'}
          ],
          price: [
            {required: true, message: '价格不能为空', trigger: 'blur'}
          ],
          disPrice: [
            {required: true, message: '节日价格不能为空', trigger: 'blur'}
          ],
          parentId: [
            {required: true, message: '上级菜单不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: false, message: '创建时间不能为空', trigger: 'blur'}
          ],
          updateTime: [
            {required: false, message: '修改时间不能为空', trigger: 'blur'}
          ],
          def0: [
            {required: false, message: '自定义字段不能为空', trigger: 'blur'}
          ],
          def1: [
            {required: false, message: '自定义字段不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    props: {
      superior: Array
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.resetFrom()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/roomtype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.floor = data.roomType.floor
                this.dataForm.name = data.roomType.name
                this.dataForm.price = data.roomType.price
                this.dataForm.type = data.roomType.type
                this.dataForm.parentId = data.roomType.parentId
                this.dataForm.createTime = data.roomType.createTime
                this.dataForm.updateTime = data.roomType.updateTime
                this.dataForm.isDelete = data.roomType.isDelete
                this.dataForm.def0 = data.roomType.def0
                this.dataForm.def1 = data.roomType.def1
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/roomtype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floor': this.dataForm.floor,
                'name': this.dataForm.name,
                'price': this.dataForm.price,
                'disPrice': this.dataForm.disPrice,
                'parentId': this.dataForm.parentId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.isLoadingVisible()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      },
      resetFrom () {
        this.dataForm.floor = ''
        this.dataForm.name = ''
        this.dataForm.price = ''
        this.dataForm.disPrice = ''
        this.dataForm.parentId = ''
        this.dataForm.type = 0
        this.dataForm.createTime = ''
        this.dataForm.updateTime = ''
        this.dataForm.isDelete = ''
        this.dataForm.def0 = ''
        this.dataForm.def1 = ''
      }
    }
  }
</script>
